// DON'T USE : tuning in progress

function DDManager () {
//================
if (instanceUnique = eval (recupSingletonCD())) 
   return instanceUnique;

this.tMissiles     = new Array

this.tTargets      = new Array;
this.tTargets['undef'] = new Object; //Array;

this.lastUnderTarget = null;
/*{oHtml:null,           
                        topMissile: null,
                        leftMissile:null
                        };
                        */
  //for multiple select, to see later ...
this.missInFlight = { tMiss : new Array
                     , top  : null
                     , left : null}

//current drag 
this.currDrag = null;

this.id= 'ID_InstanceUniqueDDManager';
}

DDManager.prototype.addTarget = function (oTarget,domain,domainInvalidTarget,idRegistTarg) {
//===========================
// domain 	: type of authorized target, with uri like syntax ; ex: Tree.Folder
// oTarget  : target object
/* traceExec('adTarget begin',74); */
var uriDom = domain || 'undef';
/* traceExec('addTarget uriDom= '+uriDom,74); */
if (uriDom.substr(0,5) != 'undef')
	uriDom = 'undef.'+uriDom;
/* traceExec('addTarget this.tTargets.length = '+this.tTargets.length,74); */
	
if (this.tTargets[uriDom] == undefined){
	/* traceExec('create uriDom = '+uriDom,74); */
	this.tTargets[uriDom] = new Object;
}
/* traceExec('this.tTargets[uriDom].id= '+this.tTargets[uriDom].id,74); */	
/* traceExec('addTarget oTarget.id= '+oTarget.id,74); */
var id = (idRegistTarg == undefined) ? oTarget.id: idRegistTarg;
/* traceExec('addTarg id= '+id,74); */
this.tTargets[uriDom][id] =  oTarget;
/* traceExec('addTarget end',74); */
}

DDManager.prototype.flyOverTarget = function (oDrag,isTargetsUnAuthorized) {
//=============================
// search an entry in target's Array 
// with entry = the longest branche as sub-branche of missile's domain
// ex: domain uri of targets = Tree.Menu 
//     is sub-branche of domain uri missile Tree.Menu.Item
//
// return adequate target for this missile 
// or null if no good target;
var goodTarg = null;
var oDrag = _DDMgr.currDrag;
var specsODrag = oDrag.oThis.specs;

var sUri = 'undef' 
// to mark autorized target
/* traceExec('fly isTargetsUnAuthorized='+isTargetsUnAuthorized,73); */
if (	(isTargetsUnAuthorized == undefined )
	||  (!isTargetsUnAuthorized)) {
	if (oDrag.oThis.specs.domain)
		sUri = sUri + '.' + oDrag.oThis.specs.domain;
}
// to mark un-autorized target
else {
	if (oDrag.oThis.specs.domainUnauthorizedTarget)
		sUri = sUri + '.' + oDrag.oThis.specs.domainUnauthorizedTarget;
}

var tUri = sUri.split('.');
/* traceExec('fly dom of missile = '+sUri,73); */

/* traceExec('== bcle / ssDom',74); */
for (var n=tUri.length; n >0; n--) {
/* traceExec('* fly test with ss dom  ='+sUri,74); */

	var targHtml, missHtml;
	if (this.tTargets[sUri]) {    // target(s) exist with(super)domain of missile 
		/* traceExec('>target exist with(super)domain of missile',74); */
          // test if oDrag is in oTarget's zone
          missHtml =  oDrag.movingOHtml;  //objetEnDeplact;
          
          var topMissile  = _rum.$posInViewPort(missHtml).top;
          var leftMissile = _rum.$posInViewPort(missHtml).left;
          
          var oTargMiss, oTarg, targHtml, topTarget, leftTarget, widthTarget, heightTarget;
          var widthMissile, heightMissile;
//          /* traceExec('flyOverTarget this.tTargets[sUri].length= '+this.tTargets[sUri].length,73); */

		  //-- examine position of any target with good sUri / position of missile
/* traceExec('===== bcle / targets with this domain : '+sUri,74); */
var j= 0;
          for (var i in this.tTargets[sUri]) {
j++;          


            oTarg = this.tTargets[sUri][i];
/* traceExec('  target '+' # '+j+ ' id = '+oTarg.id,74); */
            
            targHtml =  oTarg.oHtml;
            // target != missile (to revise must be dependant of parameter)

/*
			if (	(	(!rialto.config.isDesign)
					 && (targHtml != oDrag.oHtmlToMove))
 				||  (	rialto.config.isDesign       
 					 && (	(	(!oDrag.oHtmlToMove.ghostDesign)   //the widget in moving ( try mood)
 					 		 && (targHtml != oDrag.oHtmlToMove))    
 						 || (	(oDrag.oHtmlToMove.ghostDesign)    //a widget's ghost is moving ( design mood)
 						 	 && (targHtml != oDrag.oHtmlToMove.ghostDesign)))
 					)
				) 
 				{
*/
				
		//sufficient in design mode if this is the ghost and not the widget
		// which is moving in DD (current impl?mentation):  
		
		if (	specsODrag.circularDropAuthorized
			||  ! rialto.Dom.isDescendantOf(targHtml,oDrag.oHtmlToMove,true)) {
				//targHtml != oDrag.oHtmlToMove) {

	            if (oTarg.specsTarg.missileAsOnePixel) {
	             	widthMissile  = 1;
	              	heightMissile = 1;
	            }
	            else {
	                widthMissile = oDrag.oHtmlToMove.offsetWidth;
	               	heightMissile =oDrag.oHtmlToMove.offsetHeight;
	            }
	
	            borderExclude = !oTarg.specsTarg.borderIsTarget ;
	
				var targetPosInVP = borderExclude 
									? _rum.$divInternalTopLeftInViewPort(targHtml)
									: _rum.$posInViewPort(targHtml);
	            topTarget   = targetPosInVP.top;
	            leftTarget  = targetPosInVP.left;
	//            leftTarget  = _rum.$posInViewPort(targHtml).left;
	            
	            /* traceExec('borderExclude= '+borderExclude,79); */
	            widthTarget  = _rum.$getWidth(targHtml, bBorderLess=borderExclude);
	           	heightTarget = _rum.$getHeight (targHtml, bBorderLess=borderExclude);
	            
/*traceExec('  target '+' # '+j+ ' id = '+oTarg.id
		+ ' Miss Top= '+topMissile
		+' Targ Top= '+topTarget
		+ ' Miss right = '+(leftMissile+ widthMissile)
		+' Targ right = '+(leftTarget+ widthTarget)
		,81);*/

	/* traceExec('       Target top = '+topTarget+' left = '+leftTarget,74); */
	/* traceExec('       Missile top = '+topMissile+' left = '+leftMissile,74); */
	/* traceExec('       Target width = '+widthTarget+' height = '+heightTarget,74); */
	/* traceExec('       Missile width = '+widthMissile+' height = '+heightMissile,74); */
				// good target for this missile
				// or KO if flag isTargetsUnAuthorized == true
	            if (     (   leftMissile
	            		  >= leftTarget   )
	                 &&  (   topMissile 
	                      >= topTarget    )
	                 && (     (  leftMissile
	                           + widthMissile )
	                      <=  (  leftTarget
	                           + widthTarget ))  
	                 && (     (  topMissile
	                           + heightMissile )
	                      <=  ( topTarget
	                           + heightTarget   )) 
	                ) {
	                
	                /* traceExec('*** target '+oTarg.id+' ok test pos; isTargetsUnAuthorized= '+isTargetsUnAuthorized,74); */
	
						//already a godd target --> compare 
						if (goodTarg) {
							// this new good target is above the previous one : it's better target 
							if (DDManager.isTargetHover(oTarg, goodTarg)) {
				                // positions in target
				                goodTarg = oTarg;
				                var shiftTop =  borderExclude 
				                					? 0
				                					: _rum.$$getBorderTopWidth(targHtml);
				                var shiftLeft =  borderExclude 
				                					? 0
				                					: _rum.$$getBorderLeftWidth(targHtml);
				                 				
				                goodTarg.topInTarget  = topMissile  - topTarget - shiftTop;
				                goodTarg.leftInTarget = leftMissile - leftTarget - shiftLeft;
				
				                /* traceExec('fly 1 better  target : '+goodTarg.id+' as : '+this.lastUnderTarget.id,81); */
							}
							// the previous target is better candidate
							else {
					//			x = true;
	                			/* traceExec('fly another  target : '+oTarg.id+' but : '+goodTarg.id+ 'is better',81); */
							}
						}
						else {
			                // positions in target
			                goodTarg = oTarg;
			                var shiftTop =  borderExclude 
			                					? 0
			                					: _rum.$$getBorderTopWidth(targHtml);
			                var shiftLeft =  borderExclude 
			                					? 0
			                					: _rum.$$getBorderLeftWidth(targHtml);
			                 				
			                goodTarg.topInTarget  = topMissile  - topTarget - shiftTop;
			                goodTarg.leftInTarget = leftMissile - leftTarget - shiftLeft;
			
			                /* traceExec('fly over first target for this domain #= '+j+' id= '+goodTarg.id,81); */
		                }
		                                
		                if (specsODrag.targetChoice == 'firstIsBetter') break;
	            }
			}  //target != missile			
          } // all targets in one domain
/* traceExec('===== end of bcle / targets with this domain : '+sUri,74); */
				
	}
	if (goodTarg) {
		/* traceExec('exit good target : '+goodTarg.id,81); */
		break;
	}
/* traceExec('sUri avant = '+sUri,77); */
	sUri = tUri.slice(0,n-1).join('.');
	/* traceExec('sUri nv = '+sUri,77); */
		
} // all domains
/* traceExec('fin bcle / ssDom',74); */

// informe old target 
if (this.lastUnderTarget  && this.lastUnderTarget.DDOuter) {
    if (	!goodTarg 
        || (goodTarg != this.lastUnderTarget)) {

           this.lastUnderTarget.DDOuter(
                               { oHtml   : _DDMgr.currDrag.oHtmlToMove
            				   , topAbs  : topMissile
            				   , leftAbs : leftMissile
 	           				   }				);            	
        
    }
}    
this.lastUnderTarget = goodTarg;

// informe new target
if (goodTarg && goodTarg.DDHover)
	    goodTarg.DDHover(
	    			{  oHtml : _DDMgr.currDrag.oHtmlToMove
	                 , top   : goodTarg.topInTarget
	                 , left  : goodTarg.leftInTarget
	                }, isTargetsUnAuthorized   );


    

///* traceExec('this.lastUnderTarget.id = '+this.lastUnderTarget.id,4); */
/* traceExec('end fly _DDMgr.currDrag.cibleOk= '+_DDMgr.currDrag.cibleOk,77); */

_DDMgr.currDrag.cibleOk = (goodTarg != null) && (!isTargetsUnAuthorized);
    // ie goodTarg.cibleOk = 
    // ie this.currDrag.cibleOk = 

return goodTarg;
}
DDManager.isTargetHover = function (n1,n2) {
//===============================
// based on inheritance
if (rialto.Dom.isDescendantOf(n1,n2))
	return true;
	
// based on zIndex
else {
	return rialto.Dom.isCoveredBy(n2,n1);
}

}

DDManager.prototype.flyOverTarget_simpleVersion = function (oDrag,isTargetsUnAuthorized) {
//=============================
// search an entry in target's Array 
// with entry = the longest branche as sub-branche of missile's domain
// ex: domain uri of targets = Tree.Menu 
//     is sub-branche of domain uri missile Tree.Menu.Item
//
// return adequate target for this missile 
// or null if no good target;
var goodTarg = null;
var oDrag = _DDMgr.currDrag;

var sUri = 'undef' 
// to mark autorized target
/* traceExec('fly isTargetsUnAuthorized='+isTargetsUnAuthorized,73); */
if (	(isTargetsUnAuthorized == undefined )
	||  (!isTargetsUnAuthorized)) {
	if (oDrag.oThis.specs.domain)
		sUri = sUri + '.' + oDrag.oThis.specs.domain;
}
// to mark un-autorized target
else {
	if (oDrag.oThis.specs.domainUnauthorizedTarget)
		sUri = sUri + '.' + oDrag.oThis.specs.domainUnauthorizedTarget;
}

var tUri = sUri.split('.');
/* traceExec('fly dom of missile = '+sUri,73); */

/* traceExec('bcle / ssDom',73); */
for (var n=tUri.length; n >0; n--) {
/* traceExec('fly test with ss dom  ='+sUri,73); */

	var targHtml, missHtml;
	if (this.tTargets[sUri]) {    // target(s) exist with(super)domain of missile 
		/* traceExec('target exist with(super)domain of missile',73); */
          // test if oDrag is in oTarget's zone
          missHtml =  oDrag.movingOHtml;  //objetEnDeplact;
          
          var topMissile  = _rum.$posInViewPort(missHtml).top;
          var leftMissile = _rum.$posInViewPort(missHtml).left;
          
          var oTargMiss, oTarg, targHtml, topTarget, leftTarget, widthTarget, heightTarget;
          var widthMissile, heightMissile;
//          /* traceExec('flyOverTarget this.tTargets[sUri].length= '+this.tTargets[sUri].length,5); */

		  //-- examine position of any target with good sUri / position of missile
/* traceExec('bcle / targets',73); */
          for (var i in this.tTargets[sUri]) {
/* traceExec('-fly sUri= '+sUri+' target : '+i,73); */

            oTarg = this.tTargets[sUri][i];
            
            targHtml =  oTarg.oHtml;
            
            if (oTarg.specsTarg.missileAsOnePixel) {
             	widthMissile  = 1;
              	heightMissile = 1;
            }
            else {
                widthMissile = oDrag.oHtmlToMove.offsetWidth;
               	heightMissile =oDrag.oHtmlToMove.offsetHeight;
            }

            borderExclude = !oTarg.specsTarg.borderIsTarget ;

			var targetPosInVP = borderExclude 
								? _rum.$divInternalTopLeftInViewPort(targHtml)
								: _rum.$posInViewPort(targHtml);
            topTarget   = targetPosInVP.top;
            leftTarget  = targetPosInVP.left;
//            leftTarget  = _rum.$posInViewPort(targHtml).left;
            
            /* traceExec('borderExclude= '+borderExclude,79); */
            widthTarget  = _rum.$getWidth(targHtml, bBorderLess=borderExclude);
           	heightTarget = _rum.$getHeight (targHtml, bBorderLess=borderExclude);
            
/* traceExec('Target top = '+topTarget+' left = '+leftTarget,79); */
/* traceExec('Missile top = '+topMissile+' left = '+leftMissile,79); */
/* traceExec('Target width = '+widthTarget+' height = '+heightTarget,79); */
/* traceExec('Missile width = '+widthMissile+' height = '+heightMissile,79); */
			// good target for this missile
			// or KO if flag isTargetsUnAuthorized == true
            if (     (   leftMissile
            		  >= leftTarget   )
                 &&  (   topMissile 
                      >= topTarget    )
                 && (     (  leftMissile
                           + widthMissile )
                      <=  (  leftTarget
                           + widthTarget ))  
                 && (     (  topMissile
                           + heightMissile )
                      <=  ( topTarget
                           + heightTarget   )) 
                ) {
                
                /* traceExec('target ok test pos; isTargetsUnAuthorized= '+isTargetsUnAuthorized,73); */


	                // positions in target
	                goodTarg = oTarg;
	                var shiftTop =  borderExclude 
	                					? 0
	                					: _rum.$$getBorderTopWidth(targHtml);
	                var shiftLeft =  borderExclude 
	                					? 0
	                					: _rum.$$getBorderLeftWidth(targHtml);
	                 				
	                goodTarg.topInTarget  = topMissile  - topTarget - shiftTop;
	                goodTarg.leftInTarget = leftMissile - leftTarget - shiftLeft;
	
	                /* traceExec('fly 1 cible convenable : this.lastUnderTarget=xx',73); */
	                
	                                
	                break;
            }
		
          } // all targets in one domain
/* traceExec('fin bcle / targets',73); */
				
	}
	if (goodTarg)
		break;
/* traceExec('sUri avant = '+sUri,77); */
	sUri = tUri.slice(0,n-1).join('.');
	/* traceExec('sUri nv = '+sUri,77); */
} // all domains
/* traceExec('fin bcle / ssDom',73); */

// informe old target 
if (this.lastUnderTarget  && this.lastUnderTarget.DDOuter) {
    if (	!goodTarg 
        || (goodTarg != this.lastUnderTarget)) {

           this.lastUnderTarget.DDOuter(
                               { oHtml   : _DDMgr.currDrag.oHtmlToMove
            				   , topAbs  : topMissile
            				   , leftAbs : leftMissile
 	           				   }				);            	
        
    }
}    
this.lastUnderTarget = goodTarg;

// informe new target
if (goodTarg && goodTarg.DDHover)
	    goodTarg.DDHover(
	    			{  oHtml : _DDMgr.currDrag.oHtmlToMove
	                 , top   : goodTarg.topInTarget
	                 , left  : goodTarg.leftInTarget
	                }, isTargetsUnAuthorized   );


    

///* traceExec('this.lastUnderTarget.id = '+this.lastUnderTarget.id,4); */
/* traceExec('end fly _DDMgr.currDrag.cibleOk= '+_DDMgr.currDrag.cibleOk,77); */

_DDMgr.currDrag.cibleOk = (goodTarg != null) && (!isTargetsUnAuthorized);
    // ie goodTarg.cibleOk = 
    // ie this.currDrag.cibleOk = 

return goodTarg;
}

DDManager.prototype.drop = function (){
//======================
 if (this.lastUnderTarget) {

	 if (this.lastUnderTarget.DDOuter){
	          this.lastUnderTarget.DDOuter(
	                 { oHtml: _DDMgr.currDrag.oHtmlToMove
	                 , topAbs : _DDMgr.currDrag.topObjEnDeplact //to revise
	                 , leftAbs: _DDMgr.currDrag.leftObjEnDeplact
	                 });
	  }
	
	  if (_DDMgr.currDrag.cibleOk) {
		  /* traceExec('drop cibleOK '+this.lastUnderTarget.receiveAfterDrop,73); */
		  if (this.lastUnderTarget.receiveAfterDrop) {
		      /* traceExec('DROP receive',73); */
		     	this.lastUnderTarget.receiveAfterDrop( 
		     	                                 {    oHtml: _DDMgr.currDrag.oHtmlToMove
		     		 						   	    , top:_DDMgr.lastUnderTarget.topInTarget
		     										, left:_DDMgr.lastUnderTarget.leftInTarget
		     										, pathMissile : _DDMgr.currDrag.oThis.specs.domain
		     									  }
		     									 );
		  }
		  else { // default; to revise
		        _DDMgr.currDrag.oHtmlToMove.style.position  = 'absolute';
		        _DDMgr.currDrag.oHtmlToMove.style.top 		= _DDMgr.lastUnderTarget.topInTarget;
		        _DDMgr.currDrag.oHtmlToMove.style.left 	    = _DDMgr.lastUnderTarget.leftInTarget;   
		                 	         
		        this.lastUnderTarget.appendChild(_DDMgr.currDrag.oHtmlToMove);
		  }
	  }
      this.lastUnderTarget = null;      	         
 }
}


DDManager.prototype.getGoodTarget = function (oDrag) {
//=============================
//non used
// search an entry in target's Array 
// with entry = the longest branche as sub-branche of mssile's domain
// ex: domain uri of targets = Tree.Menu 
//     is sub-branche of domain uri missile Tree.Menu.Item
//
// return adequate target for this missile 
// or null if no good target;

var goodTarg = null;

var sUri = 'undef' 
if (oDrag.domain)
 sUri = sUri + '.' + oDrag.doamin;
 

var tUri = sUri.split('.');

for (var n=tUri.length; n >0; n--) {

	var targHtml, missHtml;
	if (this.tTargets[sUri]) {    // (super)domain of missile exist
          // test if oDrag is in oTarget's zone
          missHtml =  oDrag.oHtml;
          
          var topMissile  = _rum.$posInViewPort(missHtml).top;
          var leftMissile = _rum.$posInViewPort(missHtml).left;
          
          var oTarg, targHtml, topTarget, leftTarget;
          var widthMissile, heightMissile;
          for (var i=0; i < this.tTargets[sUri].length; i++) {
            oTarg = this.tTargets[sUri][i];

            targHtml =  oTarg.oHtml;

            if (oTarg.specsTarg.bHMinMiss) {
             	widthMissile  = 1;
              	heightMissile = 1;
            }
            else {
                widthMissile = parseInt(missHtml.style.width);
               	heightMissile = parseInt(missHtml.style.height);
            }

            topTarget   = _rum.$posInViewPort(targHtml).top;
            leftTarget  = _rum.$posInViewPort(targHtml).left;

			// good target for this missile
            if (     (   leftMissile
            		  >= leftTarget   )
                 &&  (   topMissile 
                      >= topTarget    )
                 && (     (  leftMissile
                           + widthMissile )
                      <=  (  leftTarget
                           + pixWidth(targHtml)                 ))
                 && (     (  topMissile
                           + heightMissile )
                      <=  ( topTarget
                           + pixHeight(targHtml)                ))
                ) {
                goodTarg = oTarg;
                break;
            }
		
          } // all targets in one domain
				
	}
	if (goodTarg)
		break;

	sUri = tUri.slice(0,n-1).join('.');
	
} // all domains

return goodTarg;
}

_DDMgr = new DDManager();

